<?php
class Default_Model_YourTourProcess {
	// delete user info
	function deleteUserInformation($userId) {
		$strErrMess = "";
		// delete comment
		$mComment = new Default_Model_Comment ();
		$bDelCmt = true;
		$strErrMessDelCmt = "";
		// get report of comment
		$arrComment = $mComment->getCommentByUserId($userId);
		if (count ( $arrComment ) != 0) {
			foreach ( $arrComment as $cmt ) {
				// delete photo
				$strErrMessDelCmt .= $this->deleteComment ( $cmt ['comment_id'] );
			}
		}
		if ($strErrMessDelCmt != "") {
			$bDelCmt = false;
			$strErrMess .= $strErrMessDelCmt;
		}
		
		// delete friend
		$mFriend = new Default_Model_Friend ();
		$bDelFriend = true;
		if (! $mFriend->deleteFriendByUserId ( $userId )) {
			$bDelFriend = false;
			$strErrMess .= $mFriend->getError ();
		}
		
		// delete like
		$mLike = new Default_Model_Like ();
		$bDelLike = true;
		if (! $mLike->deleteLikeByUserId ( $userId )) {
			$bDelLike = false;
			$strErrMess .= $mLike->getError ();
		}
		
		// delete location
		$mLocation = new Default_Model_Location ();
		$bDelLoc = true;
		$strErrMessDelLoc = "";
		$arrLoc = $mLocation->getLocationByUserId($userId);
		if (count ( $arrLoc ) != 0) {
			foreach ( $arrLoc as $loc ) {
				// delete photo
				$strErrMessDelLoc .= $this->deleteLocationInfo($loc['location_id']);
			}
		}
		if ($strErrMessDelLoc != "") {
			$bDelLoc = false;
			$strErrMess .= $strErrMessDelLoc;
		}
		
		// delete message
		$mMess = new Default_Model_Message ();
		$bDelMess = true;
		if (! $mMess->deleteMessageByUserId ( $userId )) {
			$bDelMess = false;
			$strErrMess .= $mMess->getError ();
		}
		
		// delete notification
		$mNotify = new Default_Model_Notification ();
		$bDelNotify = true;
		if (! $mNotify->deleteNotificationByUserId ( $userId )) {
			$bDelNotify = false;
			$strErrMess .= $mNotify->getError ();
		}
		
		// delete photo
		$mPhoto = new Default_Model_Photo ();
		$arrPhoto = $mPhoto->getPhotoByUserId ( $userId );
		$strErrMessDelPhoto = "";
		$bDelUserPhoto = true;
		if (count ( $arrPhoto ) != 0) {
			foreach ( $arrPhoto as $photo ) {
				// delete photo
				$strErrMessDelPhoto .= $this->deletePhoto ( $photo ['photo_id'] );
			}
		}
		if ($strErrMessDelPhoto != "") {
			$bDelUserPhoto = false;
			$strErrMess .= $strErrMessDelPhoto;
		}
		
		// delete tour and delete user_tour + user_location
		$mUserTour = new Default_Model_UserTour ();
		$arrMyTour = $mUserTour->getUserTour ( $userId );
		$strErrMessDelTour = "";
		$bDelUserTour = true;
		if (count ( $arrMyTour ) != 0) {
			foreach ( $arrMyTour as $tour ) {
				$strErrMessDelTour = $this->deleteTourInfo ( $tour ['tour_id'] );
			}
		}
		if ($strErrMessDelTour != "") {
			$bDelUserTour = false;
			$strErrMess .= $strErrMessDelTour;
		}
		
		// delete tour_chat
		$mTourChat = new Default_Model_TourChat ();
		$bDelTourChat = true;
		if (! $mTourChat->deleteTourChatByUserId ( $userId )) {
			$bDelTourChat = false;
			$strErrMess .= $mTourChat->getError ();
		}
		
		// after all -> delete user
		if ($bDelCmt && $bDelFriend && $bDelLike && $bDelLoc && $bDelMess && $bDelNotify && $bDelUserPhoto && $bDelUserTour && $bDelTourChat) {
			$mUser = new Default_Model_User ();
			$bDelUser = true;
			// get user info
			$arrUser = $mUser->getUserInfoById ( $userId );
			if (count ( $arrUser ) != 0) {
				$mImage = new Default_Model_Image ();
				try {
					// delete avatar
					$mImage->deleteImg ( $arrUser [0] ['profile_picture'] );
					// delete cover photo
					$mImage->deleteImg ( $arrUser [0] ['cover_photo'] );
				} catch ( Exception $e ) {
					// do nothing
				}
			}
			
			if (! $mUser->deleteUserById ( $userId )) {
				$bDelUser = false;
				$strErrMess .= $mUser->getError ();
			}
		}
		return $strErrMess;
	}
	function deleteLocationInfo($locId) {
		$strErrMess = "";
		// delete like
		$mLike = new Default_Model_Like ();
		$bDelLike = true;
		if (! $mLike->deleteLikeByLocId ( $locId )) {
			$bDelLike = false;
			$strErrMess .= $mLike->getError ();
		}
		// delete comment
		$mComment = new Default_Model_Comment ();
		$bDelCmt = true;
		$strErrMessDelCmt = "";
		// get report of comment
		$arrComment = $mComment->getCommentByLocationId ( $locId );
		if (count ( $arrComment ) != 0) {
			foreach ( $arrComment as $cmt ) {
				// delete photo
				$strErrMessDelCmt .= $this->deleteComment ( $cmt ['comment_id'] );
			}
		}
		if ($strErrMessDelCmt != "") {
			$bDelCmt = false;
			$strErrMess .= $strErrMessDelCmt;
		}
		
		// get photo of location
		$mPhoto = new Default_Model_Photo ();
		$arrPhoto = $mPhoto->getPhotoByLocId ( $locId );
		$strErrMessDelPhoto = "";
		$bDelLocPhoto = true;
		if (count ( $arrPhoto ) != 0) {
			foreach ( $arrPhoto as $photo ) {
				// delete photo
				$strErrMessDelPhoto .= $this->deletePhoto ( $photo ['photo_id'] );
			}
		}
		if ($strErrMessDelPhoto != "") {
			$bDelLocPhoto = false;
			$strErrMess .= $strErrMessDelPhoto;
		}
		
		// delete from tour_location
		$mTourLocation = new Default_Model_TourLocation ();
		$bDelTourLoc = true;
		if (! $mTourLocation->deleteTourLocationByLocId ( $locId )) {
			$bDelTourLoc = false;
			$strErrMess .= $mTourLocation->getError ();
		}
		
		// after all -> delete location
		if ($bDelLike && $bDelCmt && $bDelLocPhoto && $bDelTourLoc) {
			$mLoc = new Default_Model_Location ();
			$bDelLoc = true;
			if (! $mLoc->deleteLocationById ( $locId )) {
				$bDelLoc = false;
				$strErrMess .= $mLoc->getError ();
			}
		}
		return $strErrMess;
	}
	function deleteCityInfo($cityId) {
		$strErrMess = "";
		// after all -> delete city
		$mCity = new Default_Model_City ();
		$bDelCity = true;
		if (! $mCity->deleteCityById ( $cityId )) {
			$bDelCity = false;
			$strErrMess .= $mCity->getError ();
		}
		return $strErrMess;
	}
	function deleteTourInfo($tourId) {
		$strErrMess = "";
		
		// delete chat
		$mTourChat = new Default_Model_TourChat ();
		$bDelTourChat = true;
		if (! $mTourChat->deleteTourChatByTourId ( $tourId )) {
			$bDelTourChat = false;
			$strErrMess .= $mTourChat->getError ();
		}
		// delete photo
		// get photo of tour
		$mPhoto = new Default_Model_Photo ();
		$arrPhoto = $mPhoto->getPhotoByTourId ( $tourId );
		$strErrMessDelPhoto = "";
		$bDelTourPhoto = true;
		if (count ( $arrPhoto ) != 0) {
			foreach ( $arrPhoto as $photo ) {
				// delete photo
				$strErrMessDelPhoto .= $this->deletePhoto ( $photo ['photo_id'] );
			}
		}
		if ($strErrMessDelPhoto != "") {
			$bDelTourPhoto = false;
			$strErrMess .= $strErrMessDelPhoto;
		}
		
		// delete from tour_location table
		$bDelTourLoc = true;
		$mTourLoc = new Default_Model_TourLocation ();
		if (! $mTourLoc->deleteTourLocationByTourId ( $tourId )) {
			$bDelTourLoc = false;
			$strErrMess .= $mTourLoc->getError ();
		}
		
		// delete from user_tour table
		$bDelUserTour = true;
		if ($bDelTourLoc) {
			$mUserTour = new Default_Model_UserTour ();
			if (! $mUserTour->deleteUserTourByTourId ( $tourId )) {
				$bDelUserTour = false;
				$strErrMess .= $mUserTour->getError ();
			}
		}
		// after all -> delete tour
		if ($bDelTourChat && $bDelTourPhoto && $bDelTourLoc && $bDelUserTour) {
			$mTour = new Default_Model_Tour ();
			$bDelTour = true;
			if (! $mTour->deleteTourById ( $tourId )) {
				$bDelTour = false;
				$strErrMess .= $mTour->getError ();
			}
		}
		return $strErrMess;
	}
	function deleteComment($cmtId) {
		$strErrMess = "";
		// after all -> delete comment
		$mComment = new Default_Model_Comment ();
		$bDelCmt = true;
		// delete report
		$mReport = new Default_Model_Report ();
		$arrReport = $mReport->getReportBycmtId ( $cmtId );
		$bDelReport = true;
		$strErrMessDelReport = "";
		if (count ( $arrReport ) != 0) {
			foreach ( $arrReport as $report ) {
				if (! $mReport->deleteReportById ( $report ['report_id'] )) {
					$strErrMessDelReport .= $mReport->getError ();
				}
			}
		}
		if ($strErrMessDelReport != "") {
			$bDelReport = false;
			$strErrMess .= $strErrMessDelReport;
		}
		if ($bDelReport) {
			if (! $mComment->deleteCommentById ( $cmtId )) {
				$bDelCmt = false;
				$strErrMess .= $mComment->getError ();
			}
		}
		return $strErrMess;
	}
	function deletePhoto($photoId) {
		$strErrMess = "";
		// delete comment first
		$mComment = new Default_Model_Comment ();
		$bDelComment = true;
		if (! $mComment->deleteCommentByPhotoId ( $photoId )) {
			$strErrMess .= $mComment->getError ();
			$bDelComment = false;
		}
		
		// after all -> delete photo
		$mPhoto = new Default_Model_Photo ();
		$mImage = new Default_Model_Image ();
		$bDelPhoto = true;
		// get photo info
		$arrPhotoInfo = $mPhoto->getPhotoInfoById ( $photoId );
		if (count ( $arrPhotoInfo ) != 0 && $bDelComment) {
			// delete image from disk
			if ($mImage->deleteImg ( $arrPhotoInfo [0] ['photo_link'] )) {
				if (! $mPhoto->deletePhotoById ( $photoId )) {
					$bDelPhoto = false;
					$strErrMess .= $mPhoto->getError ();
				}
			} else {
				$bDelPhoto = false;
				$strErrMess .= $mImage->getError ();
			}
		} else {
			$bDelPhoto = false;
			$strErrMess .= 'Photo is not exist.';
		}
		return $strErrMess;
	}
}